import 'package:basic/forget_password.dart';
import 'package:basic/login.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:basic/main.dart';

class Infor extends StatefulWidget {
  final Map arguments;

  const Infor({Key key, this.arguments}) : super(key: key);

  _InforState createState() => _InforState(arguments);
}

setUserName(int id, String username) {
  SharedPreferences prefs = SpUtil.prefs;
  prefs.setInt('id', id);
  prefs.setString('username', username);
}

class _InforState extends State<Infor> {
  final Map arguments;
  _InforState(this.arguments);
  final double _appBarHeight = 170.0;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    String changeSchool;
    String changeName;
    String changeGender;
    InforMenuItem schoolItem;
    InforMenuItem name;
    InforMenuItem gender;
    schoolItem = InforMenuItem(
      title: '学校',
      tip: '未设置',
      onPressed: () {
        showDialog(
          context: context,
          builder: (context) {
            return CupertinoAlertDialog(
              title: Text('设置学校'),
              content: Card(
                elevation: 0.0,
                child: Column(
                  children: <Widget>[
                    TextField(
                      decoration: InputDecoration(
                          hintText: "请输入学校",
                          filled: true,
                          fillColor: Colors.grey.shade50),
                      onChanged: (String value) {
                        changeSchool = value;
                      },
                    )
                  ],
                ),
              ),
              actions: <Widget>[
                CupertinoDialogAction(
                  onPressed: () {
                    Navigator.pop(context);
                  },
                  child: Text('取消'),
                ),
                CupertinoDialogAction(
                  onPressed: () {
                    schoolItem.setTip(changeSchool);
                    Navigator.pop(context);
                  },
                  child: Text('确定'),
                ),
              ],
            );
          },
        );
      },
    );

    name = InforMenuItem(
      title: '姓名',
      tip: '未设置',
      onPressed: () {
        showDialog(
          context: context,
          builder: (context) {
            return CupertinoAlertDialog(
              title: Text('设置姓名'),
              content: Card(
                elevation: 0.0,
                child: Column(
                  children: <Widget>[
                    TextField(
                      decoration: InputDecoration(
                          hintText: "请输入姓名",
                          filled: true,
                          fillColor: Colors.grey.shade50),
                      onChanged: (String value) {
                        changeName = value;
                      },
                    )
                  ],
                ),
              ),
              actions: <Widget>[
                CupertinoDialogAction(
                  onPressed: () {
                    Navigator.pop(context);
                  },
                  child: Text('取消'),
                ),
                CupertinoDialogAction(
                  onPressed: () {
                    name.setTip(changeName);
                    Navigator.pop(context);
                  },
                  child: Text('确定'),
                ),
              ],
            );
          },
        );
      },
    );

    gender = InforMenuItem(
      title: '性别',
      tip: '未设置',
      onPressed: () {
        showDialog(
            context: context,
            builder: (BuildContext context) {
              return SimpleDialog(
                title: Text('选择性别'),
                children: <Widget>[
                  SimpleDialogOption(
                    child: Text('男'),
                    onPressed: () {
                      Navigator.pop(context, '男');
                    },
                  ),
                  SimpleDialogOption(
                    child: Text('女'),
                    onPressed: () {
                      Navigator.pop(context, '女');
                    },
                  ),
                ],
              );
            }).then((value) {
          print("$value" + "11111111111111111111111111111111111111111111");
          gender.setTip(value);
        });
      },
    );
    return Scaffold(
      appBar: AppBar(
        leading: new IconButton(
          icon: new Icon(Icons.arrow_back_ios),
          color: Colors.black,
          onPressed: () => {Navigator.of(context).pop('刷新')},
        ),
        centerTitle: true,
        elevation: 0,
        backgroundColor: Colors.transparent,
        title: Text(
          "个人信息",
          style: TextStyle(
            fontSize: 20.0,
            color: Colors.black54,
          ),
        ),
      ),
      body: CustomScrollView(
        slivers: <Widget>[
          SliverAppBar(
            backgroundColor: Colors.grey,
            automaticallyImplyLeading: false,
            expandedHeight: _appBarHeight,
            flexibleSpace: FlexibleSpaceBar(
              collapseMode: CollapseMode.parallax,
              background: Stack(
                fit: StackFit.expand,
                children: <Widget>[
                  Row(
                    //mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Expanded(
                        child: Column(
                          children: [
                            Padding(
                              padding: const EdgeInsets.only(
                                top: 10,
                              ),
                            ),
                            CircleAvatar(
                              radius: 35.0,
                              backgroundImage:
                                  NetworkImage(arguments['userHead']),
                            ),
                            Padding(
                              padding: const EdgeInsets.only(
                                bottom: 10.0,
                              ),
                              child: Text(
                                arguments['username'], //姓名信息暂时没有，所以先用手机号代替
                                style: TextStyle(
                                    color: Colors.white,
                                    fontWeight: FontWeight.bold,
                                    fontSize: 20.0),
                              ),
                            ),
                            Container(
                                width: 100,
                                height: 30,
                                padding: EdgeInsets.fromLTRB(2, 2, 2, 2),
                                decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(30),
                                  color: Colors.white,
                                ),
                                alignment: Alignment.center,
                                child: Text(
                                  '信用分：100  >',
                                  textAlign: TextAlign.center,
                                  style: TextStyle(
                                      fontSize: 13,
                                      fontWeight: FontWeight.bold,
                                      color: Colors.black),
                                )),
                          ],
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ),
          SliverList(
            delegate: SliverChildListDelegate(
              <Widget>[
                Container(
                  color: Colors.white,
                  margin: const EdgeInsets.only(top: 10.0),
                  child: Column(
                    children: <Widget>[
                      Container(
                        child: Column(
                          children: [
                            // InforMenuItem(
                            //   title: '姓名',
                            //   tip: arguments['username'],
                            // ),
                            name,
                            Padding(
                              padding: const EdgeInsets.only(
                                  left: 20.0, right: 20.0),
                              child: Divider(
                                color: Colors.black12,
                              ),
                            ),
                            // InforMenuItem(
                            //   title: '性别',
                            //   tip: '未设置',
                            // ),
                            gender,
                            Padding(
                              padding: const EdgeInsets.only(
                                  left: 20.0, right: 20.0),
                              child: Divider(
                                color: Colors.black12,
                              ),
                            ),
                            schoolItem,
                          ],
                        ),
                      ),
                      Container(
                        child: Column(
                          children: [
                            SizedBox(
                              height: 20,
                            ),
                            InforMenuItem(
                              title: '绑定的手机号',
                              tip: arguments['username'],
                            ),
                          ],
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.only(left: 20.0, right: 20.0),
                        child: Divider(
                          color: Colors.black12,
                        ),
                      ),
                      Container(
                        child: Column(
                          children: [
                            InforMenuItem(
                              title: '修改密码',
                              tip: ' ',
                              onPressed: () {
                                Navigator.of(context).push(MaterialPageRoute(
                                    builder: (context) => ForgetPassword()));
                              },
                            ),
                            Padding(
                              padding: const EdgeInsets.only(
                                  left: 20.0, right: 20.0),
                              child: Divider(
                                color: Colors.black12,
                              ),
                            ),
                            SizedBox(
                              height: 40,
                            ),
                            Container(
                                width: 270,
                                padding: EdgeInsets.fromLTRB(2, 2, 2, 2),
                                decoration: BoxDecoration(
                                  color: Colors.black,
                                ),
                                alignment: Alignment.center,
                                child: InkWell(
                                    onTap: () {
                                      setUserName(0, null);
                                      // Navigator.of(context).push(
                                      //   MaterialPageRoute(
                                      //     settings:
                                      //         RouteSettings(name: "/login"),
                                      //     builder: (context) => Login(),
                                      //   ),
                                      // );
                                      Navigator.pushAndRemoveUntil(
                                        context,
                                        new MaterialPageRoute(
                                            builder: (context) => new Login()),
                                        (route) => route == null,
                                      );
                                    },
                                    child: Text(
                                      '退出登录',
                                      textAlign: TextAlign.center,
                                      style: TextStyle(
                                          fontSize: 30,
                                          fontWeight: FontWeight.bold,
                                          color: Colors.white70),
                                    ))),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          )
        ],
      ),
    );
  }
}

class InforMenuItem extends StatefulWidget {
  _InforMenuItemState item;
  String title;
  VoidCallback onPressed;
  String tip;
  InforMenuItem({Key key, this.title, this.onPressed, this.tip})
      : super(key: key);
  @override
  _InforMenuItemState createState() {
    return item = _InforMenuItemState(tip, onPressed, title);
  }

  setTip(String changeTip) {
    item.setTip(changeTip);
  }
}

class _InforMenuItemState extends State<InforMenuItem> {
  String title;
  VoidCallback onPressed;
  String tip;
  _InforMenuItemState(this.tip, this.onPressed, this.title);

  setTip(String changeTip) {
    setState(() {
      tip = changeTip;
    });
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onPressed,
      child: Column(
        children: <Widget>[
          Padding(
              padding: EdgeInsets.only(
                left: 20.0,
                top: 12.0,
                right: 20.0,
                bottom: 10.0,
              ),
              child: Row(
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.only(
                      right: 8.0,
                    ),
                  ),
                  Expanded(
                      child: Text(
                    title,
                    style: TextStyle(
                        color: Colors.black54,
                        fontSize: 16.0,
                        letterSpacing: -1,
                        wordSpacing: -1),
                  )),
                  Text(
                    tip,
                  ),
                  Icon(
                    Icons.chevron_right,
                    color: Colors.grey,
                  ),
                ],
              )),
        ],
      ),
    );
  }
}
